﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using Services.BusinessRules;
namespace Services
{

    public partial class Product_UnitService
    {
       readonly IProduct_UnitEntity entity;

       public Product_UnitService()
       {
           this.entity = new Product_UnitEntity();

       }

       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model, ref string mess)
       {
           try
           {
               if(Validate((Product_Unit)_model,ref mess))
               {
               var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
               if (id == 0)
               {
                   return entity.Save((CoreData.Product_Unit)_model, Table.Product_Unit.ToString());
               }
               else
               {
                   return entity.Update((CoreData.Product_Unit)_model, Table.Product_Unit.ToString());
               }
               }
               else
               {return -1;}
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.Product_Unit GetByID(long _id)
       {
           try
           {
               return entity.GetById(_id, Table.Product_Unit.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Product_Unit> GetList()
       {
           try
           {
               return entity.GetAll(Table.Product_Unit.ToString()).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Product_Unit> GetListByLINQ(Func<CoreData.Product_Unit, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.Product_Unit.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.Product_Unit GetOneByLINQ(Func<CoreData.Product_Unit, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.Product_Unit.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Product_Unit> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetBySearchString(_searchstring, Table.Product_Unit.ToString()).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.Product_Unit)_model, Table.Product_Unit.ToString());
               return true;
           }
           catch { return false; }
       }


       #endregion

       #region SQLCommand
     
       #endregion

       #region Other Method

       bool Validate(Product_Unit obj, ref string mess)
       {
           new ValidateRequired("Mã đơn vị tính", "Code", obj.Code);
           new ValidateRequired("Tên đơn vị tính", "Name", obj.Name);
           new ValidateExist("Mã đơn vị tính", "Code", "Code", obj, GetList().Cast<object>().ToList());
           new ValidateLenght("Mã đơn vị tính", "Code", obj.Code.ToString(), 25);
           new ValidateLenght("Tên đơn vị tính", "Code", obj.Name.ToString(), 25);
           mess = BusinessObject.Validate();
           BusinessObject.Clear();
           return mess == null ? true : false;
       }

        #endregion 
    
    }
         
}
